package com.jiawa.wiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.wiki.domain.Category;
import com.jiawa.wiki.domain.CategoryExample;
import com.jiawa.wiki.mapper.CategoryMapper;
import com.jiawa.wiki.req.CategoryQueryReq;
import com.jiawa.wiki.req.CategorySaveReq;
import com.jiawa.wiki.resp.CategoryQueryResp;
import com.jiawa.wiki.resp.PageResp;
import com.jiawa.wiki.util.CopyUtil;
import com.jiawa.wiki.util.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description :
 * @Author : YTY
 * @Date : 2021-05-16 11:14
 */
@Service
public class CategoryService {

    private static final Logger LOG = LoggerFactory.getLogger(CategoryService.class);

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private SnowFlake snowFlake;

    /**
     * 一次性查询所有分类
     *
     * @return
     */
    public List<CategoryQueryResp> all() {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort ASC");
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);

        // 数据脱敏转移大法，CopyUtil 列表复制
        List<CategoryQueryResp> list = CopyUtil.copyList(categoryList, CategoryQueryResp.class);

        return list;
    }

    /**
     * 查询分类列表
     *
     * @param req
     * @return
     */
    public PageResp<CategoryQueryResp> list(CategoryQueryReq req) {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort ASC");
        // 相当于 sql where条件
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        // 传入名称不为空，才拼接查询条件
//        String bookName = req.getName();
//        if (!ObjectUtils.isEmpty(bookName)) {
//            criteria.andNameLike("%" + bookName + "%");
//        }

        // PageHelper调用startPage()方法之后，执行的第一个select语句会被分页
        PageHelper.startPage(req.getPage(), req.getSize());
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);

        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

        // 数据脱敏转移，CopyUtil 列表复制
        List<CategoryQueryResp> respList = CopyUtil.copyList(categoryList, CategoryQueryResp.class);

        PageResp<CategoryQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(respList);

        return pageResp;
    }

    /**
     * 保存分类 -> 新增 OR 更新
     */
    public void save(CategorySaveReq req) {

        // 数据转移
        Category category = CopyUtil.copy(req, Category.class);

        // 主键无数值，新增；主键有数值，更新
        if (ObjectUtils.isEmpty(req.getId())) {
            category.setId(snowFlake.nextId());
            categoryMapper.insertSelective(category);
        } else {
            categoryMapper.updateByPrimaryKey(category);
        }
    }

    /**
     * 删除分类
     */
    public void delete(Long id) {

        categoryMapper.deleteByPrimaryKey(id);
    }

}
